﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using DanielsGameLibrary;

class Water
{
    Texture2D waterBumpMap;
    Texture2D reflectionMap;
    Texture2D refractionMap;

    Vector2 vatersize;
    Vector3 pos;
    VertexPositionTexture[] waterVertices;
    protected VertexPositionTexture[] fullScreenVertices;

    ReflectionRender refrand;

    public Water(GraphicsDevice graphics, ContentManager content, Vector2 size, Vector3 p) {
        refrand = new ReflectionRender(graphics);
        waterBumpMap = content.Load<Texture2D>(@"Textures\waterbump");
        //reflectionMap = new Texture2D(graphics, 1, 1);
        refractionMap = new Texture2D(graphics, 1, 1);
        vatersize = size;
        pos = p;
        SetUpWaterVertices();
        fullScreenVertices = SetUpFullscreenVertices();
    }

    private void SetUpWaterVertices()
    {
        waterVertices = new VertexPositionTexture[6];

        waterVertices[0] = new VertexPositionTexture(new Vector3(-vatersize.X / 2, 0, vatersize.Y / 2), new Vector2(0, 1));
        waterVertices[2] = new VertexPositionTexture(new Vector3(vatersize.X / 2, 0, -vatersize.Y / 2), new Vector2(1, 0));
        waterVertices[1] = new VertexPositionTexture(new Vector3(-vatersize.X / 2, 0, -vatersize.Y / 2), new Vector2(0, 0));

        waterVertices[3] = new VertexPositionTexture(new Vector3(-vatersize.X / 2, 0, vatersize.Y / 2), new Vector2(0, 1));
        waterVertices[5] = new VertexPositionTexture(new Vector3(vatersize.X / 2, 0, vatersize.Y / 2), new Vector2(1, 1));
        waterVertices[4] = new VertexPositionTexture(new Vector3(vatersize.X / 2, 0, -vatersize.Y / 2), new Vector2(1, 0));
    }

    public void DrawMaps(GraphicsDevice device, Camera cam, GameTime time, drawSceneDelegate drawscene) { 
        reflectionMap = refrand.DrawReflectionMap( device, new Vector4(0,1,0,pos.Y),
                                                   cam , time, drawscene);
    }

    public void DrawWater(GraphicsDevice device, Effect effect, Camera cam, float time, Vector3 windDirection)
    {
        Matrix refview = ReflectionRender.getReflectionViewM(cam, new Vector4(0, 1, 0, pos.Y));
        effect.CurrentTechnique = effect.Techniques["Water"];
        Matrix worldMatrix = Matrix.Identity;
        effect.Parameters["xWorld"].SetValue(Matrix.CreateTranslation(pos));
        effect.Parameters["xView"].SetValue(cam.ViewMatrix);
        effect.Parameters["xReflectionView"].SetValue(refview);
        effect.Parameters["xProjection"].SetValue(cam.ProjectionMatrix);
        effect.Parameters["xReflectionMap"].SetValue(reflectionMap);
        effect.Parameters["xRefractionMap"].SetValue(refractionMap);
        effect.Parameters["xWaterBumpMap"].SetValue(waterBumpMap);
        effect.Parameters["xWaveLength"].SetValue(0.1f);
        effect.Parameters["xWaveHeight"].SetValue(0.3f);
        effect.Parameters["xCamPos"].SetValue(cam.position);
        effect.Parameters["xTime"].SetValue(time);
        effect.Parameters["xWindForce"].SetValue(0.00001f);
        effect.Parameters["xWindDirection"].SetValue(windDirection);
        

        foreach (EffectPass pass in effect.CurrentTechnique.Passes)
        {
            pass.Apply();
            device.DrawUserPrimitives(PrimitiveType.TriangleList, waterVertices, 0, 2, VertexPositionTexture.VertexDeclaration);
        }

        //drawstuf( device, effect, time, Vector2.Zero, 2.0f);
    }


    protected VertexPositionTexture[] SetUpFullscreenVertices()
    {
        VertexPositionTexture[] vertices = new VertexPositionTexture[4];

        vertices[0] = new VertexPositionTexture(new Vector3(-0.5f, 0.5f, 0f), new Vector2(0, 1));
        vertices[1] = new VertexPositionTexture(new Vector3(0.5f, 0.5f, 0f), new Vector2(1, 1));
        vertices[2] = new VertexPositionTexture(new Vector3(-0.5f, -0.5f, 0f), new Vector2(0, 0));
        vertices[3] = new VertexPositionTexture(new Vector3(0.5f, -0.5f, 0f), new Vector2(1, 0));

        return vertices;
    }

    private void drawstuf(GraphicsDevice device, Effect effect, float time, Vector2 speed, float overcast)
    {

        device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);

        effect.CurrentTechnique = effect.Techniques["watertest"];
        effect.Parameters["xTexture0"].SetValue(reflectionMap);
        effect.Parameters["xOvercast"].SetValue(overcast);
        effect.Parameters["xTime"].SetValue(time / 50000.0f);
     

        foreach (EffectPass pass in effect.CurrentTechnique.Passes)
        {
            pass.Apply();


            device.DrawUserPrimitives(PrimitiveType.TriangleStrip, fullScreenVertices, 0, 2);


        }
    }
}

